package year2021.month0234;

public class SynchronizedTest {

    public static void main(String[] args){
        Person person = new Person(-1);

        for (int i=0; i<10; i++){
//            person = new Person(i);
            Thread thread = new Thread(new MyThread(person, i));
            thread.start();
        }
    }

}

class MyThread implements Runnable{
    private Person person;
    private int i;

    public MyThread(Person person, int i){
        this.person = person;
        this.i = i;
    }

    @Override
    public void run() {
        person._this(i);
//        person._static(i);
    }
}

class Person{
    private static int s;
    private int i;
    public Person(int i){
        this.i = i;
    }

    public synchronized void _this(int i){
        this.i = i;
        int sum = this.sum();
        System.out.println("this："+this.i);
    }

    public void _static(int s){
        synchronized (Person.class){
//        synchronized (this){
            this.s = s;
            int sum = this.sum();
            System.out.println("static："+this.s);
        }

    }

    public int sum(){
        int sum = 0;
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*for (int k=0; k<10000; k++){
            sum = sum + k;
        }*/
        return sum;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}